﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Configuration;

namespace EWDT_CDS_Hub.App_Code
{
    public class CareGroup
    {
        public String Care_group_id { get; set; }
        public String Care_person_id { get; set; }
        public String Diploma_id { get; set; }

        public static List<CareGroup> GetCareGroup()
        {
            List<CareGroup> results = new List<CareGroup>();
            SqlConnection conn = new SqlConnection();

            try
            {
                conn.ConnectionString = ConfigurationManager.ConnectionStrings["EWDTDBString"].ConnectionString;
                conn.Open();

                SqlCommand comm = new SqlCommand();
                comm.Connection = conn;
                comm.CommandText = "SELECT * FROM CareGroup";
                SqlDataReader dr = comm.ExecuteReader();

                while (dr.Read())
                {
                    CareGroup c = new CareGroup();
                    c.Care_group_id = (String)dr["care_group_id"];
                    c.Care_person_id = (String)dr["care_person_id"];
                    c.Diploma_id = (String)dr["diploma_id"];

                    results.Add(c);
                }
            }
            catch (SqlException ex)
            {
                throw ex;
            }
            finally
            {
                conn.Close();
            }

            return results;
        }

        public static int InsertCareGroup(CareGroup c)
        {
            int rowsInserted = 0;
            SqlConnection conn = new SqlConnection();

            try
            {
                conn.ConnectionString = ConfigurationManager.ConnectionStrings["EWDTDBString"].ConnectionString;
                conn.Open();

                SqlCommand comm = new SqlCommand();
                comm.Connection = conn;
                comm.CommandText = "INSERT INTO CareGroup (care_group_id, care_person_id, diploma_id) VALUES (@cgid, @cpid, @dipid)";
                comm.Parameters.AddWithValue("@cgid", c.Care_group_id);
                comm.Parameters.AddWithValue("@cpid", c.Care_person_id);
                comm.Parameters.AddWithValue("@dipid", c.Diploma_id);
                rowsInserted = comm.ExecuteNonQuery();

            }
            catch (SqlException ex)
            {
                throw ex;
            }
            finally
            {
                conn.Close();
            }

            return rowsInserted;
        }

        public static int UpdateCareGroup(CareGroup c)
        {
            int rowsUpdated = 0;
            SqlConnection conn = new SqlConnection();

            try
            {
                conn.ConnectionString = ConfigurationManager.ConnectionStrings["EWDTDBString"].ConnectionString;
                conn.Open();
                SqlCommand comm = new SqlCommand();
                comm.Connection = conn;
                comm.CommandText = "UPDATE CareGroup SET care_person_id = @cpid, diploma_id = @did WHERE care_group_id = @id";
                comm.Parameters.AddWithValue("@id", c.Care_group_id);
                comm.Parameters.AddWithValue("@cpid", c.Care_person_id);
                comm.Parameters.AddWithValue("@did", c.Diploma_id);
                rowsUpdated = comm.ExecuteNonQuery();
            }
            catch (SqlException ex)
            {
                throw ex;
            }
            finally
            {
                conn.Close();
            }
            return rowsUpdated;
        }

        public static CareGroup RetrieveCareGroup(String id)
        {
            CareGroup c = null;
            SqlConnection conn = new SqlConnection();

            try
            {
                conn.ConnectionString = ConfigurationManager.ConnectionStrings["EWDTDBString"].ConnectionString;
                conn.Open();
                SqlCommand comm = new SqlCommand();
                comm.Connection = conn;
                comm.CommandText = "SELECT * FROM CareGroup WHERE care_group_id = @id";
                comm.Parameters.AddWithValue("@id", id);
                SqlDataReader dr = comm.ExecuteReader();

                while (dr.Read())
                {
                    c = new CareGroup();
                    c.Care_group_id = (String)dr["care_group_id"];
                    c.Care_person_id = (String)dr["care_person_id"];
                    c.Diploma_id = (String)dr["diploma_id"];
                }

                dr.Close();
            }
            catch (SqlException ex)
            {
                throw ex;
            }
            finally
            {
                conn.Close();
            }
            return c;
        }

        public static int DeleteCareGroup(String id)
        {
            int rowsDeleted = 0;
            SqlConnection conn = new SqlConnection();

            try
            {
                conn.ConnectionString = ConfigurationManager.ConnectionStrings["EWDTDBString"].ConnectionString;
                conn.Open();
                SqlCommand comm = new SqlCommand();
                comm.Connection = conn;
                comm.CommandText = "DELETE FROM CareGroup WHERE care_group_id = @id";
                comm.Parameters.AddWithValue("@id", id);
                rowsDeleted = comm.ExecuteNonQuery();
            }
            catch (SqlException ex)
            {
                throw ex;
            }
            finally
            {
                conn.Close();
            }
            return rowsDeleted;
        }
    }
}